<?php
/**
 * BButtonColumn widget
 *
 * @category  Finlandia_Coctails
 * @author    Engeny Utehin <utehin@ebola.com.ua>
 * @copyright Copyright 2013 Ebola Communications
 * @link      http://www.ebola.com.ua/
 */

Yii::import('zii.widgets.grid.CButtonColumn');

class BButtonColumn extends CButtonColumn
{

    public $viewButtonIcon      = 'glyphicon glyphicon-search';
    public $updateButtonIcon    = 'glyphicon glyphicon-pencil';
    public $deleteButtonIcon    = 'glyphicon glyphicon-trash';

    public $viewButtonOptions   = array('class' => 'btn btn-info btn-xs');
    public $updateButtonOptions = array('class' => 'btn btn-success btn-xs');
    public $deleteButtonOptions = array('class' => 'btn btn-danger btn-xs delete');

    private $_defaultButtons = array('view', 'update', 'delete');

    /**
     * @see CButtonColumn::registerClientScript()
     */
    protected function registerClientScript()
    {
        $js = array();

        foreach ($this->buttons as $id => $button) {
                if (isset($button['click'])) {
                    $function     = CJavaScript::encode($button['click']);
                    $class        = preg_replace('/\s+/','.', $button['options']['class']);
                    $confirmation = '';

                    if (!in_array($id, $this->_defaultButtons) && isset($button['confirmation'])) {
                        $confirmation="if(!confirm(".CJavaScript::encode($button['confirmation']).")) return false;";
                    }

                    if (in_array($id, $this->_defaultButtons)) {
                        $js[]="jQuery(document).on('click','#{$this->grid->id} a.{$class}',$function);";
                    } else {
                        $js[] = 'jQuery(document).on("click","#' . $this->grid->id .
                            ' a.' . $class . '", function(e){' . $confirmation . ' ' . $function . '});';
                    }
                }
        }

        if($js !== array())
            Yii::app()->getClientScript()->registerScript(__CLASS__.'#'.$this->id, implode("\n",$js));
    }

    /**
     * @see CButtonColumn::initDefaultButtons()
     */
    protected function initDefaultButtons()
    {
        parent::initDefaultButtons();

        foreach ($this->_defaultButtons as $id) {
            $button = array(
                'icon' => $this->{$id.'ButtonIcon'},
            );
            if (isset($this->buttons[$id]))
                $this->buttons[$id] = array_merge($button, $this->buttons[$id]);
            else
                $this->buttons[$id] = $button;
        }
    }

    /**
     * @see CButtonColumn::renderButton()
     */
    protected function renderButton($id,$button,$row,$data)
    {
        if (isset($button['visible']) && !$this->evaluateExpression($button['visible'],
                                            array('row'=>$row,'data'=>$data)))
            return;

        $label   = isset($button['label']) ? $button['label'] : $id;
        $url     = isset($button['url']) ? $this->evaluateExpression($button['url'],
                                    array('data' => $data, 'row' => $row)) : '#';
        $options = isset($button['options']) ? $button['options'] : array();

        if (!isset($options['title']))
            $options['title'] = $label;
        if (isset($button['icon']) && is_string($button['icon'])) {
            echo CHtml::link(
                CHtml::openTag('icon', array('class' => $button['icon'])) .
                CHtml::closeTag('icon'), $url, $options);
        } else {
            echo CHtml::link($label, $url, $options);
        }
    }
}
